package mysql

import (
	"short-video-manager/videorpc/videorpc"

	"gorm.io/gorm"
)

type CollectionInfo struct {
	gorm.Model
	UserId           int `gorm:"type:int(11);not null;comment:用户Id"`
	VideoId          int `gorm:"type:int(11);not null;comment:视频id"`
	CollectionStatus int `gorm:"int(11);comment:收藏状态"`
}

func (c *CollectionInfo) CollectionAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *CollectionInfo) CollectFind(db *gorm.DB, in *videorpc.CollectDelRequest) error {
	return db.Debug().Where("user_id=? and video_id=?", in.UserId, in.VideoId).Find(c).Error
}

func (c *CollectionInfo) CollectDel(db *gorm.DB) error {
	return db.Debug().Delete(c).Error
}

func (c *CollectionInfo) CollectionFind(db *gorm.DB, in *videorpc.CollectionRequest) error {
	return db.Debug().Where("user_id=? and video_id=?", in.UserId, in.VideoId).Find(c).Error
}

func CollectList(db *gorm.DB, in *videorpc.CollectListRequest) []*videorpc.CollectList {
	var list []*videorpc.CollectList
	db.Debug().Model(&CollectionInfo{}).Select("nick_name,cover_line,video_title AS title,play_nym as play_num").
		Joins("LEFT JOIN user_infos ON collection_infos.user_id = user_infos.id").
		Joins("LEFT JOIN video_infos ON collection_infos.video_id=video_infos.id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&list)
	return list
}
